package action.login;

import java.util.Map;

import org.apache.struts2.ServletActionContext;

import model.User;

import DAO.UserDAO;
import DAO.DAOImple.UserDAOImple;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


public class LoginAction extends ActionSupport {
	private User user;
	
	public String execute(){
		
		if(user == null) {
			return ERROR;
		}
		
		ActionContext act = ActionContext.getContext();
		UserDAO userdao = new UserDAOImple();
		
		int result = userdao.userLogin(user); //用于用户登录 
		
		if(result == 0) {
			act.put("namewrong", "用户名错误");
			return ERROR;
		}else if(result == 1) {
			act.put("passwrong","密码错误");
			return ERROR;
		}else{
			user = userdao.getUser(user.getId());
			act.getSession().put("user", user);   //登陆成功
			return SUCCESS;
		}
		
	}

	public User getUser() {
		return user;
	}	
	
	public void setUser(User user) {
		this.user = user;
	}
}
